package com.qf.controller;

import com.qf.enums.ResultEnum;
import com.qf.exception.AppException;
import com.qf.pojo.DevUser;
import com.qf.service.DevUserService;
import com.qf.utils.ResultVoUtil;
import com.qf.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import static com.qf.constant.AppConstant.DEV_USER_LOGIN;
import static com.qf.constant.AppConstant.DEV_USER_REGISTER;

@Controller
@RequestMapping("/dev/user")
@Slf4j

public class DevUserController  {
    @Autowired
    DevUserService devUserService;

    //跳转到注册页面
    @GetMapping("/register-ui")
    public String registerUI(){

        return DEV_USER_REGISTER;
    }

    @PostMapping("/check-username")
    @ResponseBody
    public ResultVo checkUsername(String username){
        if (StringUtils.isEmpty(username)){
            log.info("[]参数不合法 username={}.",username);
            throw new AppException(ResultEnum.PARAM_ERROR);
        }

        Integer data = devUserService.checkUsername(username);
        return ResultVoUtil.success(data);

    }

    //执行注册功能
    @PostMapping("/register")
    @ResponseBody
    public ResultVo register(@Valid DevUser devUser, BindingResult bindingResult) throws MessagingException {

        if (bindingResult.hasErrors()){
            String msg = bindingResult.getFieldError().getDefaultMessage();
            log.info("[注册账号] 参数不合法 devUser={}.", devUser);
            throw new AppException(ResultEnum.PARAM_ERROR.getCode(), msg);
        }

        devUserService.register(devUser);
        return ResultVoUtil.success();
    }

    @GetMapping("/active")
    public String active(@RequestParam String devUsername,
                         @RequestParam String code,
                         Model model){


        try {
            devUserService.active(devUsername,code);
            model.addAttribute("success", "<a href='/dev/user/login-ui>点我登录</a>'");
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }

    }

    @GetMapping("/login-ui")
    public String loginUI(){
        return DEV_USER_LOGIN;
    }

    //重新发送用户的激活邮件
    @GetMapping("/resend-active-mail")
    @ResponseBody
    public ResultVo resendActiceMail(String devUsername) throws MessagingException {
        if (StringUtils.isEmpty(devUsername)){
            log.info("[重新发送激活邮件] 参数不合法.devUsername={}",devUsername );
            throw new AppException(ResultEnum.PARAM_ERROR);
        }

        devUserService.resendActiveMail(devUsername);
        return ResultVoUtil.success();
    }




    // 跳转到重新发送激活账号的页面
    @GetMapping("/active-ui")
    public String activeUI(){
        return "dev/user/active";
    }

    @PostMapping("/login")
    @ResponseBody
    public ResultVo login(String devUsername, String devPassword, HttpSession session){
        if (StringUtils.isEmpty(devUsername)||StringUtils.isEmpty(devPassword)){
            log.info("[登录功能] 参数不合法 devUsername={},devPassword={}}", devUsername,
                    devPassword );
            throw new AppException(22,"登录失败");
        }

        Integer username = devUserService.checkUsername(devUsername);
        if (username!=1){
            log.info("该用户不存在",29 );
            throw new AppException(29,"该用户不存在" );
        }

        DevUser devUser=devUserService.login(devUsername,devPassword);

        //登录这个放session个人觉得目的是为了下次登录避免验证
        //登录的本质是查用户名和密码如果和数据库一致就跳转首页 不一致提示用户名或密码错误
        session.setAttribute("devUser", devUser);
        return ResultVoUtil.success();

    }





}
